#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    SpanTooLong,
    InvalidDigit(char),
}

pub fn lsp(string_digits: &str, span: usize) -> Result<u64, Error> {
    if string_digits.len() < span { return Err(Error::SpanTooLong); }
    let mut result: u64 = 0;
    for i in 0..=string_digits.len() - span {
        let series = string_digits.get(i..i + span).unwrap();
        let mut tmp = 1;
        for ch in series.chars() {
            match ch.to_digit(10) {
                None => { return Err(Error::InvalidDigit(ch)); },
                Some(num) => {
                    tmp *= num as u64;
                }
            }
        }
        if tmp > result { result = tmp}
    }
    Ok(result)
}

pub fn lsp1(string_digits: &str, span: usize) -> Result<u64, Error> {
    match span {
        0 => Ok(1),
        _ => string_digits
            .chars()
            .map(|c| c.to_digit(10).ok_or(Error::InvalidDigit(c)).map(u64::from))
            .collect::<Result<Vec<_>, _>>()?
            .windows(span)
            .map(|w| w.iter().product())
            .max()
            .ok_or(Error::SpanTooLong),
    }
}

